﻿function MobilePhone() {}
MobilePhone.textBox = new Function();
MobilePhone.textBox.onKeyPress = function (sender, e) {
	return (48 <= e.keyCode && e.keyCode <= 57);
}
MobilePhone.textBox.onBlur = function (sender, minLength, maxLength) {
	if (sender.value && isNaN(sender.value)) {
		alert("숫자만 입력할 수 있습니다.");
		sender.focus();
		sender.select();
		return false;
	}
	document.getElementById(sender.id.substring(0, sender.id.length - 2)).invalidate();
	return true;
}
MobilePhone.dropDownList = new Function();
MobilePhone.dropDownList.onBlur = function (sender) {
	if (sender.selectedIndex == 0 && sender.requiredFieldValidation == "True") {
		alert("필수 선택 사항입니다.");
		sender.focus();
		return false;
	}
	document.getElementById(sender.id.substring(0, sender.id.length - 2)).invalidate();
	return true;
}
MobilePhone.dropDownList.onChange = function (sender) {
}
MobilePhone.initialize = function (id) {
	var c = document.getElementById(id);
	var c1 = document.getElementById(id + "_1");
	var c2 = document.getElementById(id + "_2");
	var c3 = document.getElementById(id + "_3");
	c1.validate = function() {
		return (this.requiredFieldValidation != "True" || this.selectedIndex > 0);
	}
	c2.validate = function() {
		if (this.requiredFieldValidation != "True" && this.value.length == 0) {
			return true;
		} else {
			return this.value.length == 3 || this.value.length == 4;
		}
	}
	c3.validate = function() {
		if (this.requiredFieldValidation != "True" && this.value.length == 0) {
			return true;
		} else {
			return this.value.length == 4;
		}
	}
	c.validate = function () {
		var c1 = document.getElementById(this.id + "_1");
		var c2 = document.getElementById(this.id + "_2");
		var c3 = document.getElementById(this.id + "_3");
		if (!c1.validate()) { alert(this.errorMessage); c1.focus(); return false; }
		if (!c2.validate()) { alert(this.errorMessage); c2.focus(); c2.select(); return false; }
		if (!c3.validate()) { alert(this.errorMessage); c3.focus(); c3.select(); return false; }
		if (this.requiredFieldValidation != "True" && this.value.length == 0) {
			return true;
		} else {
			if (this.value.length == 10 || this.value.length == 11) {
				return true;
			} else {
				alert(this.errorMessage);
				if (c1.selectedIndex == 0) { c1.focus(); return false; }
				if (c2.value.length == 0) { c2.focus(); c2.select(); return false; }
				// c3 case
				c3.focus();
				c3.select();
				return false;
			}
		}
	}
	c.invalidate = function () {		
		var c1 = document.getElementById(this.id + "_1");
		var c2 = document.getElementById(this.id + "_2");
		var c3 = document.getElementById(this.id + "_3");
		this.value = c1[c1.selectedIndex].value + c2.value + c3.value;
	}
}
